package com.iweb.comment.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.iweb.comment.dao.CommentDao;
import com.iweb.comment.pojo.Comment;
import com.iweb.comment.service.CommentService;
import com.iweb.common.page.DataGrid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Optional;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentDao commentDao;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public DataGrid<Comment> findCommentsByArticleId(String articleId, Integer current,
                                                     Integer size) {
        DataGrid<Comment> dataGrid = new DataGrid<>();
        // 封装查询条件
        Comment comment = new Comment();
        comment.setArticleId(articleId);
        Example<Comment> example = Example.of(comment);
        // 封装分页和排序
        Pageable pageable = PageRequest.of(current - 1, size, Sort.Direction.DESC, "publishDate");
        // 查询数据
        Page<Comment> comments = commentDao.findAll(example, pageable);
        // 设置数据
        dataGrid.setTotal(comments.getTotalElements());
        dataGrid.setRows(comments.getContent());

        return dataGrid;
    }

    @Override
    public void addComment(Comment comment) {
        // 分布式ID生成器生成id
        comment.set_id(IdWorker.getIdStr());
        // 设置评论时间
        comment.setPublishDate(LocalDateTime.now());
        // 设置评论点赞数
        comment.setThumbUp(0);
        commentDao.insert(comment);
    }

    @Override
    public Comment updateComment(Comment comment) {
        // 设置修改评论的时间
        comment.setPublishDate(LocalDateTime.now());
        // 根据id查询需要修改的评论
        Optional<Comment> commentOptional = commentDao.findById(comment.get_id());
        Comment commentSave = commentOptional.get();
        // 将修改的参数注入要保存的数据,忽略null
        BeanUtil.copyProperties(comment, commentSave, CopyOptions.create().ignoreNullValue());
        // 修改评论
        commentDao.save(commentSave);
        return commentSave;
    }

    @Override
    public void deleteCommentById(String commentId) {
        commentDao.deleteById(commentId);
    }

    @Override
    public boolean thumpUp(String userId, String commentId) {
        // 到redis中确认是否已经点赞 key thump_up 保存到一个set集合中 value = userId_commentId
        String key = "thump_up";
        String value = userId + "_" + commentId;
        Boolean thumpUp = redisTemplate.boundSetOps(key).isMember(value);
        if (thumpUp) {
            return false;
        }
        // 封装查询条件
        Query query = Query.query(Criteria.where("_id").is(commentId));
        // 封装修改规则
        Update update = new Update();
        update.inc("thumbUp", 1);
        // 通过文章ID对评论进行增1
        mongoTemplate.updateFirst(query, update, Comment.class);
        // 保存评论关系到redis
        redisTemplate.boundSetOps(key).add(value);
        return true;
    }
}
